<script>
	$('.link').unbind('click').on('click',function(){
		var url = $(this).attr('link');
		if(!url) return false;
		location.href = url ;
	});
	
	$(function(){

        initDelFile();
        initDelFile2();
        $(".js_upload_img").each(function(k, v){
            var $this = $(this);
            var id = $this.find('input').attr('id');
            var upviewer = $this.find('.upviewer');
            var val = '';
            upviewer.find('img').each(function(i, j){
                val += val ? ","+$(j).attr('src') : $(j).attr('src');
            });
            upviewer.append('<input class="uploadval" type="hidden" name="'+id+'" id="'+id+'" value="'+val+'"></input>');
            //多文件上传
            initUploadify({ 
                ID:id,
                url:"{:U('Upload/upload')}",
                btnText : '<button type="button" class="btn btn-primary "><span class="glyphicon glyphicon-plus">上传单张图片</span></button>',
                success:function(obj){ 
                    if(obj.s){
                        upviewer.find("ul").html('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
                        upviewer.find("#"+id).val(obj.d);
                        initDelFile2();
                    }else{
                        alert(obj.m);
                    }
                } 
            });
        });

        $(".js_upload_imgs").each(function(k, v){
            var $this = $(this);
            var id = $this.find('input').attr('id');
            var upviewer = $this.find('.upviewer');
            var val = '';
            upviewer.find('img').each(function(i, j){
                val += val ? ","+$(j).attr('src') : $(j).attr('src');
            });
            upviewer.append('<input class="uploadval" type="hidden" name="'+id+'" id="'+id+'" value="'+val+'"></input>');
            //多文件上传
            initUploadify({
                ID: id,
                url:"{:U('Upload/upload')}",
                btnText : '<button type="button" class="btn btn-primary "><span class="glyphicon glyphicon-plus">上传多张图片</span></button>',
                success: function(obj){ 
                    if(obj.s){
                        upviewer.find("ul").append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
                        var val = '';
                        upviewer.find('img').each(function(i, j){
                            val += val ? ","+$(j).attr('src') : $(j).attr('src');
                        });
                        upviewer.find("#"+id).val(val);
                        initDelFile2();
                    }else{
                        alert(obj.m);
                    }
                } 
            });
        });

        $(".js_upload_video").each(function(k, v){
            var $this = $(this);
            var id = $this.find('input').attr('id');
            var upviewer = $this.find('.upviewer');
            var val = '';
            upviewer.find('video').each(function(i, j){
                val += val ? ","+$(j).attr('src') : $(j).attr('src');
            });
            upviewer.append('<input class="uploadval" type="hidden" name="'+id+'" id="'+id+'" value="'+val+'"></input>');
            //多文件上传
            initUploadify({ 
                ID:id,
                url:"{:U('Upload/upload')}?dir=videos",
                fType:"*.mp4",
                sizeLimit:"50MB",
                btnText : '<button type="button" class="btn btn-primary "><span class="glyphicon glyphicon-plus">上传视频</span></button>',
                success:function(obj){ 
                    if(obj.s){
                        upviewer.find("ul").html('<li style="height:300px;width: auto;"><video controls style="height:280px;" src="'+obj.d+'"></video><a d_path="'+obj.d+'">删除</a></li>');
                        upviewer.find("#"+id).val(obj.d);
                        initDelFile2();
                    }else{
                        alert(obj.m);
                    }
                } 
            });
        });

        function initDelFile(){
            $('.upviewer li a').unbind('click').click(function(){
                if(!confirm('你确认删除吗?')) return false;
                var _this = $(this);
                var dpath = $(_this).attr('d_path');
                if(!dpath) return false;
                $.post("{:U('Upload/delfile')}",{filepath:dpath},function(obj){ 
                    if(obj.s){ 
                        $(_this).parents('li').remove();
                    }else{
                        alert(obj.m);
                        $(_this).parents('li').remove();
                    } 
                },'json');
            });
        }
        function initDelFile2(){
            $('.upviewer li a').unbind('click').click(function(){
                if(!confirm('你确认删除吗?')) return false;
                var _this = $(this);
                var dpath = $(_this).attr('d_path');
                if(!dpath) return false;
                $.post("{:U('Upload/delfile')}",{filepath:dpath},function(obj){
                    var upviewer = _this.parents('.upviewer');
                    if(obj.s){ 
                        $(_this).parents('li').remove();
                    }else{
                        alert(obj.m);
                        $(_this).parents('li').remove();
                    }
                    var val = '';
                    upviewer.find('img').each(function(i, j){
                        val += val ? ","+$(j).attr('src') : $(j).attr('src');
                    });
                    upviewer.find('.uploadval').val(val);
                },'json');
            });
        }
    })
</script>
</body>
</html>